using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;
/// <summary> 
/// Summary description for BasePage 
/// </summary> 

namespace webapp4U.UI
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            //if (Session["MyTheme"] == null)
            //{
            //    Session.Add("MyTheme", "Default");
            //    Page.Theme = ((string)Session["MyTheme"]);
            //}
            //else
            //{
            //    Page.Theme = ((string)Session["MyTheme"]);
            //}
            Page.Theme = "Default";
        }

        protected override void InitializeCulture()
        {

            
            String selectedLanguage;


            if (Request["lang"] != null)
                Session["lang"] = Request["lang"];

            if (Session["lang"] == null)
                Session["lang"] = "vi-VN";

            if (Session["lang"].ToString().ToLower() == "vi-vn")
            {
                selectedLanguage = "vi-VN";
            }
            else
            {
                selectedLanguage = "en-US";
            }



            

            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

            base.InitializeCulture();
        }


        protected override void OnLoad(EventArgs e)
        {
            // add onfocus and onblur javascripts to all input controls on the forum,
            // so that the active control has a difference appearance
            //Helpers.SetInputControlsHighlight(this, "highlight", false);

            base.OnLoad(e);
        }


        public string ApplicationPath
        {
            get
            {
                string url = this.Request.ApplicationPath;
                if (url.EndsWith("/"))
                    return url;
                else
                    return url + "/";
            }
        }


        public string FullApplicationPath
        {
            get
            {
                return this.Request.Url.AbsoluteUri.Replace(
                   this.Request.Url.PathAndQuery, "") + this.ApplicationPath;
            }
        }


        public String PhysicalApplicationPath()
        {
            return HttpContext.Current.Request.PhysicalApplicationPath.ToString();
        }

        protected void RequestLogin()
        {
            this.Response.Redirect(FormsAuthentication.LoginUrl +
               "?ReturnUrl=" + this.Request.Url.PathAndQuery);
        }


        public string FormatDate(DateTime date)
        {
            return date.ToString("MM/dd/yy");
        }

        public static string FormatDouble(double num)
        {
            return num.ToString("#,#").Replace(",", ".");
        }


        public static int GetIntFromQueryString(string key)
        {
            int returnValue = -1;
            string queryStringValue;

            // Attempt to get the value from the query string
            //
            queryStringValue = HttpContext.Current.Request.QueryString[key];

            // If we didn't find anything, just return
            //
            if (queryStringValue == null)
                return returnValue;

            // Found a value, attempt to conver to integer
            //
            try
            {

                // Special case if we find a # in the value
                //
                if (queryStringValue.IndexOf("#") > 0)
                    queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

                returnValue = Convert.ToInt32(queryStringValue);
            }
            catch { }

            return returnValue;

        }


        public static string GetStringFromQueryString(string key)
        {
            string returnValue = "";
            string queryStringValue;

            // Attempt to get the value from the query string
            //
            queryStringValue = HttpContext.Current.Request.QueryString[key];

            // If we didn't find anything, just return
            //
            if (queryStringValue == null)
                return returnValue;

            // Found a value, attempt to conver to integer
            //
            try
            {

                // Special case if we find a # in the value
                //
                if (queryStringValue.IndexOf("#") > 0)
                    queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

                returnValue = Convert.ToString(queryStringValue);
            }
            catch { }

            return returnValue;
        }
    }
}



